/*
RetroLeague - Mobile application for The Retro League Podcast
Copyright (C) 2010-2013 Hugues Johnson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.huguesjohnson.retroleague.fb;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public abstract class FacebookJSONParser{
	private final static String TAG="FacebookJSONParser";

	public static String parseLatestStatus(String json){
		String latestStatus="";
		try{
			String startTag="\"message\":\"";
			int indexOf=json.indexOf(startTag);
			if(indexOf>0){
				int start=indexOf+startTag.length();
				String endTag="\",";
				return(json.substring(start,json.indexOf(endTag,start)));
			}
		}catch(Exception x){
			Log.e(TAG,"parseLatestStatus",x);
		}
		return(latestStatus);
	}
	
	public static ArrayList<AlbumItem> parseAlbums(String json) throws JSONException{
		ArrayList<AlbumItem> albums=new ArrayList<AlbumItem>();
		try{
			JSONObject rootObj=new JSONObject(json);
			JSONArray itemList=rootObj.getJSONArray("data");
			int albumCount=itemList.length();
			for(int albumIndex=0;albumIndex<albumCount;albumIndex++){
				JSONObject album=itemList.getJSONObject(albumIndex);
				String description="";
				try{
					description=album.getString("description");
				}catch(JSONException x){
					Log.d(TAG,"parseAlbums",x);
				}
				albums.add(new AlbumItem(album.getString("id"),album.getString("name"),description));
			}
		}catch(Exception x){
			Log.e(TAG,"parseAlbums",x);
		}
		return(albums);
	}

	public static ArrayList<PhotoItem> parsePhotos(String json) throws JSONException{
		ArrayList<PhotoItem> photos=new ArrayList<PhotoItem>();
		try{
			JSONObject rootObj=new JSONObject(json);
			JSONArray itemList=rootObj.getJSONArray("data");
			int photoCount=itemList.length();
			for(int photoIndex=0;photoIndex<photoCount;photoIndex++){
				JSONObject photo=itemList.getJSONObject(photoIndex);
				photos.add(new PhotoItem(photo.getString("picture"),photo.getString("source")));
			}
		}catch(Exception x){
			Log.e(TAG,"parsePhotos",x);
		}
		return(photos);
	}
}